<%-- 
    Document   : reset-password-action
    Created on : Feb 28, 2012, 1:11:23 AM
    Author     : PhucAn
--%>
<%@page import="util.MailUtil"%>
<%@page import="util.GeneratorUtil"%>
<%@page import="util.SharingUtil"%>
<%@page import="dto.User"%>
<%@page import="dao.UserDAO"%>
<%
    if (request.getParameter("send") != null) {
        // String username=request.getParameter("username");
        String email = request.getParameter("email");
        UserDAO daoUser = new UserDAO();
        User user = daoUser.getUser(email);
        //RequestDispatcher rd=application.getRequestDispatcher("/OnlineQuiz");
        if (user.getId() > 0) {
            String randomString = GeneratorUtil.getRandomPassword(20);
            String subject = "Online Quiz: Reset password";
            String body = "You have request to reset you password from Online Quiz. Please follow this link to receive your new password :";
            body += ("http://localhost:8080/OnlineQuiz/public?pcontent=rstpwd-confirm&code=" + randomString+"&uid="+user.getId());
            user.setActivationCode(randomString);
            if (daoUser.updateActivationCode(user) > 0) {
                MailUtil mailer = new MailUtil();
                if (mailer.send(application, email, subject, body) > 0) {
                    SharingUtil.showMessage(session, "An email will be sent to you. Please check the email and confirm it to reset your password");
                }else{
                    SharingUtil.showMessage(session, "Sorry. The server cannot finish your request now. Please try later");
                }
            }
        } else {
            SharingUtil.showMessage(session, "There is no account with specified email.");
        }
    }
%>